﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LdExpress;
using System.Data;
using MySql.Data.MySqlClient;

public partial class _Default : System.Web.UI.Page
{
    public string strCondition = "";
    public int NumOfItemsPerPage = 20;
    public string strPager = "";
    public string strError = "";
    public string strMess = "";
    public int nPermissions = 0;
    public string strLetter = "letter";
    public string strAlphaBet = "";
    string strChar = "";
    public int spdVOL_Sum = 0;
    public double spdGW_Sum = 0;
    public double spdMEAS_Sum = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        int nPage = 1;
        DataTable dtList;
        int nNumOfPages = 0;
        if (Session["logined_id"] != null)
        {
            if (Session["cusdoc"] != "") nPermissions = int.Parse(Session["cusdoc"].ToString());

        }
        else
            Response.Redirect("Login.aspx");
        if (!Page.IsPostBack)
        {
            BuildSPDList(strCondition, -1, nPage, out nNumOfPages, out dtList, null, null);
            rptShippingDetail.DataSource = dtList;
            rptShippingDetail.DataBind();
            LoadCusDoc();
        }


    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        //  Response.Redirect("CreateCusdoc.aspx?ID=" + cusdocid.ToString());
        CusdocModel obj = new CusdocModel();
        obj.ID = long.Parse(Request.QueryString["ID"].ToString());
        obj.Load(GV.cn, GV.trans);
        obj.SI_ATTN = txtSIATTN.Text.Trim();
        obj.SI_COMMENT = txtSICOMMENT.Text.Trim();
        obj.SI_NOTED = txtSINOTED.Text.Trim();
        obj.SI_UPDATE = DateTime.Now;
        obj.UpdateShipingIntruction(GV.cn, GV.trans);
        Response.Redirect("ShippingInstruction.aspx?ID=" + Request.QueryString["ID"].ToString());
    }
    private void LoadCusDoc()
    {
        if (Request.QueryString["ID"] != null)
        {
            long cusdocID = long.Parse(Request.QueryString["ID"].ToString());
            CusdocModel obj = new CusdocModel();
            obj.ID = cusdocID;
            obj.Load(GV.cn, GV.trans);
            string strCarrn = obj.CARRN;

            if (strCarrn.Length > 0 && (strCarrn.IndexOf("\n") > 0))
            {
                strCarrn = strCarrn.Substring(0, strCarrn.IndexOf("\n") + 1);
            }
            lblCARRN.Text = strCarrn;
            lblMBKGNO.Text = obj.MBKGNO;
            txtSIATTN.Text = obj.SI_ATTN;
            lblSIUPDATE.Text = obj.SI_UPDATE.ToString("dd MMMM yyyy");
            txtSICOMMENT.Text = obj.SI_COMMENT;
            txtSINOTED.Text = obj.SI_NOTED;
            lblMBLSHPR.Text = obj.MBLSHPR.Replace("\n", "<br>");
            lblMBLCNEE.Text = obj.MBLCNEE.Replace("\n", "<br>"); ;
            lblMBLNTF.Text = obj.MBLNTF.Replace("\n", "<br>"); ;
            txtDOGMN.Text = obj.DOGMN.Replace("\n", "<br>"); ;
            //st2
            txtVSSL.Text = obj.VSSL;
            txtVOYNO.Text = obj.VOYNO;
            txtPOLID.Text = obj.POLNAME.ToString();//Display POLNAME
            txtDESTID.Text = obj.DESTNAME.ToString();//Display DESTNAME

            //st3
            lblTOP.Text = obj.TOP;
        }

    }
    private void BuildSPDList(string strCondition, int NumOfItemsPerPage, int PageNo, out int nNumOfPages, out DataTable dtList, MySqlConnection cn, MySqlTransaction trans)
    {
        DataSet ds = null;
        dtList = new DataTable();
        dtList.Columns.Add("ID");
        dtList.Columns.Add("INDEX");
        dtList.Columns.Add("CONT");
        dtList.Columns.Add("SEAL");
        dtList.Columns.Add("ContType");
        dtList.Columns.Add("VOL");
        dtList.Columns.Add("PKGS");
        dtList.Columns.Add("GW");
        dtList.Columns.Add("MEAS");
        dtList.Columns.Add("TEMP");
        dtList.Columns.Add("Action");
        nNumOfPages = 0;

        string[] strValues = null;
        if (Request.QueryString["ID"] != null)
        {
            strCondition += " AND CusdocID=" + Request.QueryString["ID"].ToString();

            ShippingDetail.GetList(strCondition, " ID ASC", NumOfItemsPerPage, PageNo, out nNumOfPages, out ds, GV.cn, GV.trans);

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                int count = 0;
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    count++;
                    string ID = dr["ID"].ToString();
                    string strIndex = count.ToString();

                    string CONT = dr["CONT"].ToString();
                    string SEAL = dr["SEAL"].ToString();
                    string ContType = dr["ContType"].ToString();
                    string PKGS = dr["PKGS"].ToString();
                    string VOL = dr["VOL"].ToString();
                    if (VOL == "0") VOL = "";
                    string GW = "";
                    double dGW = double.Parse(dr["GW"].ToString());
                    if (dGW > 0)
                        GW = dGW.ToString("0,0.##");

                    string MEAS = "";
                    double dMEAS = double.Parse(dr["MEAS"].ToString());
                    if (dMEAS > 0)
                        MEAS = dMEAS.ToString("0,0.##");
                    if (!dr.IsNull("VOL"))
                        spdVOL_Sum += int.Parse(dr["VOL"].ToString());
                    spdGW_Sum += dGW;
                    spdMEAS_Sum += dMEAS;
                    string TEMP = dr["TEMP"].ToString();
                    string Action = " <a href=\"javascript:doDeleteSPR('" + Request.QueryString["ID"].ToString() + "','" + dr["ID"].ToString() + "')\">DEL</a>";

                    strValues = new string[] { ID, strIndex, CONT, SEAL, ContType, VOL, PKGS, GW, MEAS, TEMP, Action };
                    dtList.Rows.Add(strValues);
                }
            }
        }
    }
}